package main

import (
	"fmt"
	"time"

	"gxgeek.com/gxgeek/learngo/lang/retrieveriever/mock"
	"gxgeek.com/gxgeek/learngo/lang/retrieveriever/real"
)

const url  = "http://www.imooc.com"
type Retriever interface {
	Get(url string) string
}
type Poster interface {
	Post(url string,from map[string]string) string
}

func download(r Retriever) string {
	return r.Get(url)
}

func post(r Poster) string {
	return r.Post(url,
			map[string]string{
				"name":"gk",
				"course":"golang",
			},
		)
}

type RetrieverPoster interface {
	Retriever
	Poster
}

func session(s RetrieverPoster)  string{
	s.Post(url,map[string]string{
		"contents":"another faked imooc.com",
	})

	return s.Get(url)

}

func main() {
	var r Retriever
	//var r  mock.Retriever
	retriever := mock.Retriever{"T1213121"}
	r = &retriever
	inspect(r)
	r = &real.Retriever{
		UserAgent: "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0",
		TimeOut:   time.Minute,
	}
	inspect(r)

	if mockRetriever,ok := r.(*mock.Retriever);ok{
		fmt.Println(mockRetriever.Contents)
	}else {
		fmt.Println("not mockRetriever ")
	}

	retriever = mock.Retriever{"T1213121"}

	fmt.Println("Try a session ")
	fmt.Println(session(&retriever))
	//realRetriever := r.(*real.Retriever)
	//fmt.Println(realRetriever.TimeOut)
	//fmt.Println(download(r))
	//fmt.Println(download(r))
}
func inspect(r Retriever) {
	fmt.Println("Inspecting  ",r)
	fmt.Printf("> %T %v \n", r, r)
	switch v := r.(type) {
	case *mock.Retriever:
		fmt.Println("Contents: 	", v.Contents)
	case *real.Retriever:
		fmt.Println("UserAgent: 	", v.UserAgent)

	}
	fmt.Println()
}
